package org.oms.wechater.apis;

import org.oms.wechater.enums.EnumMethod;
import org.oms.wechater.utils.HttpRequestUtil;

import net.sf.json.JSONObject;

public class OAuth2API {
	private static final String get_oauth2_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE&agentid=AGENTID";

	/**
	 * 根据code获取成员信息
	 * 
	 * @param token
	 * @param code
	 * @param AgentID
	 * @return
	 */
	public static JSONObject getUserByCode(String token, String code, int AgentID) {

		JSONObject jo = null;
		try {
			jo = doGetUser(token, code, AgentID);
		} catch (Exception e) {
			try {
				jo = doGetUser(token, code, AgentID);
			} catch (Exception ex) {
			}
		}
		return jo;
	}

	private static JSONObject doGetUser(String token, String code, int AgentID) {
		String menuUrl = get_oauth2_url.replace("ACCESS_TOKEN", token).replace("CODE", code).replace("AGENTID", AgentID + "");
		JSONObject jo = HttpRequestUtil.httpRequest(menuUrl, EnumMethod.GET.name(), null);
		System.out.println("jo=" + jo);
		return jo;
	}

}
